package com.across.connect.util;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xiaonei.api.ProfileField;
import com.xiaonei.api.XiaoneiRestClient;
import com.xiaonei.api.schema.FriendsGetAppFriendsResponse;
import com.xiaonei.api.schema.User;
import com.xiaonei.api.schema.UserAccount;
import com.xiaonei.api.schema.UsersGetInfoResponse;

public class XnConnect {

	private static final String XN_API = CommonConst.XN_API_KEY;
	private static final String XN_SECRECT = CommonConst.XN_API_SECRET;
	private static Map<Integer, String> XN_KEY_MAP = new HashMap<Integer, String>();

	public static String getXiaoneiApiKey() {
		return XN_API;
	}

	public static void setXiaoneiSessionKey(int xnuid, String key) {
		XN_KEY_MAP.put(xnuid, key);
	}

	/**
	 * 向校内注册用户
	 * 
	 * @param email
	 */
	public static void xiaonei_registerUser(List<UserAccount> accounts) {
		try {
			XiaoneiRestClient client = new XiaoneiRestClient(XN_API, XN_SECRECT);
			client.connect_registerUsers(accounts);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 向校内注册用户
	 * 
	 * @param email
	 */
	public static void xiaonei_registerUser(UserAccount account) {
		List<UserAccount> accounts = new ArrayList<UserAccount>();
		accounts.add(account);
		XnConnect.xiaonei_registerUser(accounts);
	}

	/**
	 * 卸载用户
	 * 
	 * @param email
	 */
	public static void xiaonei_unregisterUsers(String[] email,int hostid) {
		try {
			XiaoneiRestClient client = new XiaoneiRestClient(XN_API,
					XN_SECRECT, XN_KEY_MAP.get(hostid));
			List<UserAccount> accounts = new ArrayList<UserAccount>();
			for (String str : email) {
				UserAccount ua = new UserAccount();
				ua.setEmailHash(EmailHashUtil.getEmailHash(str));
				accounts.add(ua);
			}
			client.connect_unregisterUsers(accounts);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取当前登录者ID
	 * 
	 * @return 校内ID
	 */
	public static int getXiaoneiOnlineUID(int hostid) {
		try {
			XiaoneiRestClient client = new XiaoneiRestClient(XN_API,
					XN_SECRECT, XN_KEY_MAP.get(hostid));
			return client.users_getLoggedInUser();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	/**
	 * 获取校内用户EmailHash
	 * 
	 * @param 校内用户ID
	 * @return
	 */
	public static String getXiaoneiUserEmailHashes(int hostid) {
		try {
			XiaoneiRestClient client = new XiaoneiRestClient(XN_API,
					XN_SECRECT, XN_KEY_MAP.get(hostid));
			EnumSet<ProfileField> profile = EnumSet.of(ProfileField.EMAIL_HASH);
			client.users_getInfo(hostid, profile);
			UsersGetInfoResponse reps = (UsersGetInfoResponse) client
					.getResponsePOJO();
			return reps.getUser().get(0).getEmailHash();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public static List<Integer> getXiaoneiAPPFriends(int hostid) {
		try {
			XiaoneiRestClient client = new XiaoneiRestClient(XN_API,
					XN_SECRECT, XN_KEY_MAP.get(hostid));
			client.friends_getAppFriends();
			FriendsGetAppFriendsResponse resp = (FriendsGetAppFriendsResponse) client
					.getResponsePOJO();
			return resp.getUid();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static List<User> getXiaoneiUserInfo(List<Integer> uids, int hostid){
		try {
			XiaoneiRestClient client = new XiaoneiRestClient(XN_API,
					XN_SECRECT, XN_KEY_MAP.get(hostid));
			EnumSet<ProfileField> profile = EnumSet.allOf(ProfileField.class);
			client.users_getInfo(uids, profile);
			UsersGetInfoResponse reps = (UsersGetInfoResponse) client
					.getResponsePOJO();
			return reps.getUser();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static User getXiaoneiUserInfo(int uid, int hostid){
		try {
			List<Integer> uids = new ArrayList<Integer>();
			uids.add(uid);
			return getXiaoneiUserInfo(uids, hostid).get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String args[]) {
		
		//XnConnect.setXiaoneiSessionKey("2.dd0ed88eb5231eb932cd972f8fb1a47e.3600.1240916400-230121017");
//		List<Integer> list = getXiaoneiAPPFriends();
//		for (Integer friend : list) {
//			System.out.println(friend);
//		}
//		UserAccount account = new UserAccount();
//		account.setEmailHash(EmailHashUtil.getEmailHash("ivor2008@gmail.com"));
//		xiaonei_registerUser(account);
		//System.out.println(getXiaoneiUserEmailHashes(230121017));
		//xiaonei_unregisterUsers(new String[]{"ivor2008@gmail.com"});
		//getXiaoneiOnlineUID();
		setXiaoneiSessionKey(273114374, "3.027d2cfaa06cf401df29a5da1c87548a.3600.1241006400-273114374");
		getXiaoneiUserEmailHashes(273114374);
	}
}
